package org.raymond.iworks.study.mq.kafka.service;

import lombok.extern.slf4j.Slf4j;
import org.raymond.iworks.apis.avro.LogModel;
import org.raymond.iworks.commons.constants.MQConstants;
import org.raymond.iworks.study.spring.service.BaseTestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

import java.time.Instant;

/**
 * @author raymond
 * @version V1.0
 * @Description: TODO
 */
@Slf4j
@Service
public class MQService {
    @Autowired
    private BaseTestService baseTestService;
    @Autowired
    private KafkaTemplate kafkaTemplate;

    public void delayTaskTest(){
        log.info("send delay message start");
        kafkaTemplate.send(MQConstants.TOPIC_LOG, "this is a delay task");
        log.info("send delay end");

        // 测试logConsumer批量接收消息
        for(long i=1; i<6; i++) {
            Long current = Instant.now().toEpochMilli();
            LogModel logModel = new LogModel();
            logModel.setId(i);
            logModel.setRequestIP("ip");
            logModel.setRequestURI("uri");
            logModel.setMethod("post");
            logModel.setClientTime(current);
            logModel.setHeaders("headers");
            logModel.setParams("params");
            logModel.setStatus("200");
            logModel.setStartTime(current);
            logModel.setEndTime(current);
            new Thread(()->{
                kafkaTemplate.send(MQConstants.TOPIC_LOG, logModel);
            }).start();
        }
        log.info("send log end");
    }
}
